/*
 * @Desc: 全局信息
 * @Author: feim
 * @return: {Object}
 * @Date: 2022-06-22 19:16:02
*/
import db from '@/utils/storage';

export default {
  namespaced: true,
  state: {
    sidebar: {
      opened: true,
      withoutAnimation:false
    },
    device: 'desktop',
    size: 'medium',
    downloadFresh: 0,
    pageJump : 0,
    userVersion: db.get('userVersion') || 0,
    userInfo: {}
  },

  getters: {
    getUserInfo (state) {
      return state.userInfo;
    },
    getDownloadFresh (state) {
      return state.downloadFresh;
    },
    getPageJump (state) {
      return state.pageJump;
    },
  },

  actions: {
    setUserInfo ({ commit },userInfo) {
      commit('mergeState', { userInfo });
    },

    setDownloadFresh({ commit }, downloadFresh) {
      commit('mergeState', { downloadFresh });
    },
    setPageJump({ commit }, pageJump) {
      commit('mergeState', { pageJump });
    },
    setUserVersion({ commit }, userVersion) {
      db.set('userVersion',userVersion);
      commit('mergeState', { userVersion });
    },
    toggleSideBar({ commit }) {
      commit('TOGGLE_SIDERBAR')
    },
    closeSideBar({ commit }, { withoutAnimation }) {
      commit('CLOSE_SIDEBAR', withoutAnimation)
    },
    openSideBar({ commit }, { withoutAnimation }) {
      commit('OPEN_SIDEBAR', withoutAnimation)
    },
    setSize({ commit }, size) {
      commit('SET_SIZE', size)
    }
  },
  mutations: {
    /**
     * 通用改变状态的值
     * @param {Object} state        状态
     * @param {Object} mergeObject  需要改变的对象 {key: value}
     */
    mergeState(state, mergeObject) {
      for (const key in mergeObject) {
        state[key] = mergeObject[key];
      }
    },
    TOGGLE_SIDERBAR (state) {
      state.sidebar.opened = !state.sidebar.opened
      state.sidebar.withoutAnimation = false
    },
    CLOSE_SIDEBAR: (state, withoutAnimation) => {
      state.sidebar.opened = false
      state.sidebar.withoutAnimation = withoutAnimation
    },
    OPEN_SIDEBAR: (state, withoutAnimation) => {
      state.sidebar.opened = true
      state.sidebar.withoutAnimation = withoutAnimation
    },
    SET_SIZE: (state, size) => {
      state.size = size
    }
  }
};
